package com.meego.common.oembed;

import java.util.ArrayList;
import java.util.List;

public class OembedProviderBuilder {
	private final DefaultOembedProvider oembedProvider = new DefaultOembedProvider();
	
	public OembedProviderBuilder withName(final String name) {
		this.oembedProvider.setName(name);
		return this;
	}
	
	public OembedProviderBuilder withExtraParam(final List<String> params) {
		for (String string : params) {
			String[] param = string.split("=");
			if(param.length == 2)
				this.oembedProvider.addExtraParam(param[0], param[1]);
		}
		return this;
	}
	
	public OembedProviderBuilder withFormat(final String format) {
		this.oembedProvider.setFormat(format);
		return this;
	}
	
	public OembedProviderBuilder withEndpoint(final String endpoint) {
		this.oembedProvider.setEndpoint(endpoint);
		return this;
	}
	
	public OembedProviderBuilder withUrlSchemes(final String... urlSchemes) {
		if(urlSchemes != null) {
			if(this.oembedProvider.getUrlSchemes() == null)
				this.oembedProvider.setUrlSchemes(new ArrayList<String>());
			for(String urlScheme : urlSchemes)
				this.oembedProvider.getUrlSchemes().add(urlScheme);
		}
		return this;
	}
	
	public OembedProviderBuilder withMaxHeight(final Integer maxHeight) {
		this.oembedProvider.setMaxHeight(maxHeight);
		return this;
	}
	
	public OembedProviderBuilder withMaxWidth(final Integer maxWidth) {
		if(maxWidth != null && maxWidth > 100)
			this.oembedProvider.setMaxWidth(maxWidth);
		return this;
	}

	public OembedProvider build() {
		return oembedProvider;
	}
}